/*
 * Copyright (c) 2011 Anthony Vandikas
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stdafx.h"
#include "StudentWidget.h"

#include "AbstractHistory.h"
#include "AbstractStudentList.h"
#include "StudentListModel.h"

StudentWidget::StudentWidget(AbstractHistory &history, AbstractStudentList &studentList)
	: history(&history), studentList(&studentList), model(new StudentListModel(studentList))
{
	ui.setupUi(this);
	ui.studentView->setModel(model);
}

StudentWidget::~StudentWidget()
{
	delete model;
}

void StudentWidget::openStudentList()
{
	auto filename = QFileDialog::getOpenFileName(
		this,
		QString(),
		QString(),
		"Csv Files (*.csv)");

	// Null filenames indicate that the user clicked cancel,
	// in which case there is no point in continuing.
	if (filename.isNull())
		return;

	auto func = [=](QTextStream *stream) -> QList<Student>
	{
		// The csv file should have a header, which should be skipped.
		if (!stream->atEnd())
			stream->readLine();

		QList<Student> output;
		while (!stream->atEnd())
		{
			auto data = stream->readLine().split(',');
			auto identifier = data[0];
			auto name = data[1] + " " + data[2];

			output << Student(name, identifier, QString::null);
		}

		return output;
	};

	bool success;
	QList<Student> students;
	std::tie(success, students) = useFileText<QList<Student>>(filename, QFile::ReadOnly, func);
	
	if (!success)
		QMessageBox::warning(this, "Error", "Error reading file.", QMessageBox::Ok, QMessageBox::NoButton);

	studentList->reset(students);
}

void StudentWidget::resetPasswords()
{
	if (QMessageBox::question(this, "InventoryMan", "Are you sure you want to reset the passwords for the selected students?", QMessageBox::Yes, QMessageBox::No) != QMessageBox::Yes)
		return;

	foreach (auto index, ui.studentView->selectionModel()->selectedIndexes())
	{	
		auto student = model->getStudent(index);
		studentList->setPassword(student.getIdentifier(), QString::null);
	}
}

void StudentWidget::clearHistory()
{
	if (QMessageBox::question(this, "InventoryMan", "Are you sure you want to clear all history?", QMessageBox::Yes, QMessageBox::No) != QMessageBox::Yes)
		return;

	history->clear();
}
